// /api/login
// /api/logout 

const http = require('http');
const qs = require('querystring');
http.createServer((req, res) => {
  var url = req.url;
  var rawdata = '';
  console.log('method:', req.method);

  res.setHeader('Content-Type', 'application/json;charset=utf-8;')
  req.on('data', (chunk) => {
    rawdata += chunk;
  })
  req.on('end', () => {
    console.log(url);
    switch (url) {
      case '/api/login':
        let username = qs.parse(rawdata).username;
        var result = {
          code: 1,
          message: '登录成功',
          username
        }
        res.end(JSON.stringify(result));
        break;
      case "/api/logout":
        var result = {
          code: 1,
          message: '退出成功'
        }
        res.write(JSON.stringify(result));
        res.end();
    }

  })


}).listen(3000, () => {
  console.log('3000 start ...')
})